package com.yhh.common.base.ret;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
public class ApiResult implements Serializable {
    public static final int SUCCESS_CODE=200;
    public static final int ERROR_CODE=200;
    public static final int ILLEGAL_CODE=200;

    public static final String SUCCESS_MSG="操作成功";
    public static final String ERROR_MSG="服务器异常";
    public static final String ILLEGAL_MSG="参数非法";

    private int code;
    private String message;
    private Map<String,Object> result = new HashMap<>();

    private ApiResult(){}

    /**
     * 操作成功
     * @return
     */
    public static ApiResult success(){
        return ApiResult.success(SUCCESS_MSG);
    }

    /**
     * 服务器异常
     * @return
     */
    public static ApiResult error(){
        return ApiResult.error(ERROR_MSG);
    }

    /**
     * 操作成功
     * @return
     */
    public static ApiResult success(String msg){
        ApiResult ret = new ApiResult();
        ret.code = SUCCESS_CODE;
        ret.message = msg;
        return ret;
    }

    /**
     * 服务器异常
     * @return
     */
    public static ApiResult error(String msg){
        ApiResult ret = new ApiResult();
        ret.code = ERROR_CODE;
        ret.message = msg;
        return ret;
    }

    /**
     * 参数非法
     * @return
     */
    public static ApiResult illegal(){
        ApiResult ret = new ApiResult();
        ret.code = ILLEGAL_CODE;
        ret.message = ILLEGAL_MSG;
        return ret;
    }

    public ApiResult put(String key, Object value){
        this.result.put(key, value);
        return this;
    }

    public ApiResult setMsg(String msg){
        this.message = msg;
        return this;
    }
}
